package com.group.app.example.util;


import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SensitiveInfoUtil {

    public static String chineseName(String fullname){
        if(StringUtils.isEmpty(fullname)){
            return "";
        }else{
            fullname = fullname.replace("（","(");
            fullname = fullname.replace("）",")");
            if (fullname.length() < 3){
                return StringUtils.left(fullname,1)+StringUtils.repeat("*",fullname.length()-1);
            }
            if(fullname.contains("(")&&fullname.contains(")")){
                Pattern pattern;
                Matcher matcher;
                if(fullname.contains(":")||fullname.contains("：")){
                    fullname =fullname.replace("：",":");
                    pattern=Pattern.compile("\\(.*[\\:](.*?)\\)");
                    matcher =pattern.matcher(fullname);
                    if (matcher.find()){
                        return fullname.substring(0,fullname.indexOf(":")+1)+chineseName(matcher.group(1))+fullname.substring(fullname.indexOf(")"));
                    }else {
                        pattern=Pattern.compile("\\((.*?)\\)");
                        matcher=pattern.matcher(fullname);
                        return fullname.substring(0,fullname.indexOf(":")+1)+chineseName(matcher.group(1))+fullname.substring(fullname.indexOf(")"));
                    }
                }
                return "";
            }
            if(fullname.contains("·")){
                int index=fullname.indexOf("·");
                return chineseName(fullname.substring(0,index))+fullname.substring(index);
            }
            return StringUtils.left(fullname,1)+StringUtils.repeat("*",fullname.length()-1);
        }
    }

    /**
     * 身份证号
     * @param idCard
     * @return
     */
    public static String idCard(String idCard){
        if(StringUtils.isEmpty(idCard)){
            return "";
        }else {
            return StringUtils.left(idCard,4)+StringUtils.repeat("*",idCard.length()-7)+StringUtils.right(idCard,3);
        }
    }

    /**
     * 手机号
     * @param phoneNum
     * @return
     */
    public static  String phoneNum(String phoneNum){
        if(StringUtils.isEmpty(phoneNum)){
            return "";
        }else {
            return StringUtils.left(phoneNum,3)+StringUtils.repeat("*",phoneNum.length()-7)+StringUtils.right(phoneNum,4);
        }
    }
    /**
     * 银行卡证号
     * @param bankCard
     * @return
     */
    public static String bankCard(String bankCard){
        if(StringUtils.isEmpty(bankCard)){
            return "";
        }else {
            return StringUtils.left(bankCard,4)+StringUtils.repeat("*",bankCard.length()-8)+StringUtils.right(bankCard,4);
        }
    }
}
